{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "45.5 ms ± 4.59 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)\n"
     ]
    }
   ],
   "source": [
    "%%timeit\n",
    "n = 1000000\n",
    "sum = 0\n",
    "for i in range(n):\n",
    "    sum += i\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.68 ms ± 210 μs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)\n"
     ]
    }
   ],
   "source": [
    "%%timeit\n",
    "n = 1000000\n",
    "i = np.arange(n)\n",
    "sum = np.sum(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "35.27131782945737"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "45.5/1.29"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4, 5, 6, 7, 8]"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = [1,2,3,4]\n",
    "b = [5,6,7,8]\n",
    "a + b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['h', 'e', 'l', 'l', 'o', 'w', 'o', 'r', 'l', 'd']"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = list('hello')\n",
    "b = list('world')\n",
    "a + b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[6, 8, 10, 12]"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = [1,2,3,4]\n",
    "b = [5,6,7,8]\n",
    "c = []\n",
    "for i in range(len(a)):\n",
    "    c.append(a[i]+b[i])\n",
    "c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[6, 8, 10, 12]"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = [1,2,3,4]\n",
    "b = [5,6,7,8]\n",
    "c = [a[i]+b[i] for i in range(len(a))]\n",
    "c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = np.array([1,2,3,4])\n",
    "b = np.array([5,6,7,8])\n",
    "c = a + b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-0.2794155 ,  0.98935825, -0.54402111, -0.53657292])"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sin(c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([   403.42879349,   2980.95798704,  22026.46579481, 162754.791419  ])"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.exp(c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1.79175947, 2.07944154, 2.30258509, 2.48490665])"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.log(c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0.96017029, -0.14550003, -0.83907153,  0.84385396])"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.cos(c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 36,  64, 100, 144])"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c**2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2.44948974, 2.82842712, 3.16227766, 3.46410162])"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c**0.5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype('int32')"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c.dtype"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = np.array([1,2,3,4], 'float32')\n",
    "b = np.array([3.5,4.5,5.5,6.5],'float32')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype('float32')"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.dtype"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype('float64')"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b.dtype"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [],
   "source": [
    "c = a + b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype('float32')"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c.dtype"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.zeros(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [],
   "source": [
    "d = np.zeros([3,3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(3, 3)"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.ones(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.empty(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.arange(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 3, 5, 7, 9])"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.arange(1,10,2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.        , 0.11111111, 0.22222222, 0.33333333, 0.44444444,\n",
       "       0.55555556, 0.66666667, 0.77777778, 0.88888889, 1.        ])"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.linspace(0,1,10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1. ])"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.linspace(0,1,11)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1. ])"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.arange(0,1.1,0.1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.7864981514073631"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.randn()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {},
   "outputs": [],
   "source": [
    "data1d = np.array([1, 2, 3])               # 一维数组\n",
    "data2d = np.array([[1,2], [3,4]])          # 二维数组\n",
    "mixed_type = np.array([1, \"two\", 3.0])     # 类型自动提升 → dtype='<U21'\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 78,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data2d[1,1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {},
   "outputs": [],
   "source": [
    "c = [[1,2], [3,4]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 81,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c[1][1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 2. 特殊矩阵生成\n",
    "zeros_arr = np.zeros((2,3))                # 2行3列浮点零矩阵\n",
    "ones_int = np.ones(3, dtype=np.int32)   # 含三个1的整型数组\n",
    "full_arr = np.full((2,2), 9.9)             # 2x2全9.9矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[90., 90.],\n",
       "       [90., 90.]])"
      ]
     },
     "execution_count": 95,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.full((2,2), 90.0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = 1+1j"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1+1j)"
      ]
     },
     "execution_count": 97,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "metadata": {},
   "outputs": [],
   "source": [
    "complex_arr = np.array([1, 2], dtype=np.complex128)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1.+0.j, 2.+0.j])"
      ]
     },
     "execution_count": 106,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "complex_arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-2147483648"
      ]
     },
     "execution_count": 109,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "-2**31"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2147483647"
      ]
     },
     "execution_count": 110,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "2**31-1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "自动推导形状:\n",
      " [[ 0.          0.90909091  1.81818182  2.72727273]\n",
      " [ 3.63636364  4.54545455  5.45454545  6.36363636]\n",
      " [ 7.27272727  8.18181818  9.09090909 10.        ]]\n",
      "\n",
      "拷贝填充后矩阵:\n",
      " [[9. 8. 7.]\n",
      " [6. 5. 4.]\n",
      " [3. 2. 1.]]\n"
     ]
    }
   ],
   "source": [
    "# 调整数组维度\n",
    "original = np.arange(24)\n",
    "reshaped = original.reshape(4, 6)  # 4行6列矩阵\n",
    "\n",
    "# 自动推导维度\n",
    "auto_shape = np.linspace(0,10,12).reshape(3, -1)  # 3x4矩阵\n",
    "\n",
    "# 初始化后填充\n",
    "empty_matrix = np.empty((3,3))    # 分配内存但不初始化\n",
    "np.copyto(empty_matrix, [[9,8,7],[6,5,4],[3,2,1]])\n",
    "\n",
    "print(\"自动推导形状:\\n\", auto_shape)\n",
    "print(\"\\n拷贝填充后矩阵:\\n\", empty_matrix)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 116,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 调整数组维度\n",
    "original = np.arange(24)\n",
    "reshaped = original.reshape(4, 6)  # 4行6列矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 113,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,\n",
       "       17, 18, 19, 20, 21, 22, 23])"
      ]
     },
     "execution_count": 113,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "original"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 119,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 自动推导维度\n",
    "auto_shape = np.linspace(0,10,12).reshape(3, 2, -1)  # 3x4矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 120,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[ 0.        ,  0.90909091],\n",
       "        [ 1.81818182,  2.72727273]],\n",
       "\n",
       "       [[ 3.63636364,  4.54545455],\n",
       "        [ 5.45454545,  6.36363636]],\n",
       "\n",
       "       [[ 7.27272727,  8.18181818],\n",
       "        [ 9.09090909, 10.        ]]])"
      ]
     },
     "execution_count": 120,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "auto_shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 122,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原始数组:\n",
      " [[2 7 0 3]\n",
      " [9 7 0 4]\n",
      " [9 8 0 1]]\n",
      "\n",
      "核心属性:\n",
      "数组维度: 2\n",
      "形状信息: (3, 4)\n",
      "元素总数: 12\n",
      "数据类型: int32\n",
      "单个元素字节: 4\n",
      "存储顺序:   C_CONTIGUOUS : True\n",
      "  F_CONTIGUOUS : False\n",
      "  OWNDATA : True\n",
      "  WRITEABLE : True\n",
      "  ALIGNED : True\n",
      "  WRITEBACKIFCOPY : False\n",
      "\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "# 创建3x4随机整数数组（0-9）\n",
    "arr = np.random.randint(0, 10, size=(3,4))\n",
    "print(\"原始数组:\\n\", arr)\n",
    "print(\"\\n核心属性:\")\n",
    "print(\"数组维度:\", arr.ndim)     # 2\n",
    "print(\"形状信息:\", arr.shape)    # (3,4)\n",
    "print(\"元素总数:\", arr.size)     # 12\n",
    "print(\"数据类型:\", arr.dtype)    # int32\n",
    "print(\"单个元素字节:\", arr.itemsize)  # 4\n",
    "print(\"存储顺序:\", arr.flags)     # 展示内存布局信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 123,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-1自动推导数组:\n",
      " [[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]]\n",
      "\n",
      "展平方法:\n",
      "flatten结果: [ 0  1  2  3  4  5  6  7  8  9 10 11]\n",
      "ravel结果: [ 0  1  2  3  4  5  6  7  8  9 10 11]\n"
     ]
    }
   ],
   "source": [
    "# 基础形状变换\n",
    "arr1d = np.arange(12)            # [0 1 2 ...11]\n",
    "\n",
    "# 行列转换\n",
    "matrix = arr1d.reshape(3,4)       # 转3x4二维数组\n",
    "tensor = matrix.reshape(2,2,3)    # 转为2x2x3三维数组\n",
    "\n",
    "# 自动推算维度\n",
    "auto_shape = arr1d.reshape(3, -1)  # 自动计算第二维为4\n",
    "print(\"-1自动推导数组:\\n\", auto_shape)\n",
    "\n",
    "# 展平操作对比\n",
    "print(\"\\n展平方法:\")\n",
    "print(\"flatten结果:\", matrix.flatten())  # 使用拷贝（复制数据）\n",
    "print(\"ravel结果:\", matrix.ravel())      # 视图（原始数据引用）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 124,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "一维示例:\n",
      "4号元素: 4\n",
      "步长切片: [1 3 5]\n",
      "\n",
      "二维数组:\n",
      "首行: [0 1 2 3]\n",
      "末列: [ 3  7 11]\n",
      "区域访问: [[4 5]\n",
      " [8 9]]\n",
      "\n",
      "布尔筛选:\n",
      " [ 8  9 10 11]\n",
      "\n",
      "导步切片影响原数组:\n",
      " [[99  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]]\n"
     ]
    }
   ],
   "source": [
    "# 一维数组索引\n",
    "arr = np.arange(10)               # [0 1 2 3 4 5 6 7 8 9]\n",
    "print(\"一维示例:\")\n",
    "print(\"4号元素:\", arr[4])         # 4\n",
    "print(\"步长切片:\", arr[1:7:2])     # [1 3 5]\n",
    "\n",
    "# 多维数组访问\n",
    "matrix = np.arange(12).reshape(3,4)\n",
    "print(\"\\n二维数组:\")\n",
    "print(\"首行:\", matrix[0])         # [0 1 2 3]\n",
    "print(\"末列:\", matrix[:,-1])      # [3 7 11]\n",
    "print(\"区域访问:\", matrix[1:, :2]) # [[4,5], [8,9]]\n",
    "\n",
    "# 布尔索引\n",
    "filter_arr = matrix[matrix > 7]   # 保留所有大于7的元素\n",
    "print(\"\\n布尔筛选:\\n\", filter_arr)  # [ 8  9 10 11]\n",
    "\n",
    "# 导步索引（视图）\n",
    "slice_view = matrix[::2, ::2]     # 使用导步生成视图\n",
    "slice_view[0,0] = 99             # 修改会影响原始数组\n",
    "print(\"\\n导步切片影响原数组:\\n\", matrix)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 131,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3],\n",
       "       [ 4,  5,  6,  7],\n",
       "       [ 8,  9, 10, 11]])"
      ]
     },
     "execution_count": 131,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "matrix = np.arange(12).reshape(3,4)\n",
    "matrix"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 135,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 6,  7,  8,  9, 10, 11])"
      ]
     },
     "execution_count": 135,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "filter_arr = matrix[matrix > 5]   # 保留所有大于7的元素\n",
    "filter_arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 136,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[False, False, False, False],\n",
       "       [False, False,  True,  True],\n",
       "       [ True,  True,  True,  True]])"
      ]
     },
     "execution_count": 136,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "matrix > 5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 137,
   "metadata": {},
   "outputs": [],
   "source": [
    "slice_view = matrix[::2, ::2]     # 使用导步生成视图"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 138,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  2],\n",
       "       [ 8, 10]])"
      ]
     },
     "execution_count": 138,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "slice_view"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 142,
   "metadata": {},
   "outputs": [],
   "source": [
    "slice_view[1,1] = 99             # 修改会影响原始数组\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 143,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[99,  2],\n",
       "       [ 8, 99]])"
      ]
     },
     "execution_count": 143,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "slice_view"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 144,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[99,  1,  2,  3],\n",
       "       [ 4,  5,  6,  7],\n",
       "       [ 8,  9, 99, 11]])"
      ]
     },
     "execution_count": 144,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "matrix"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 145,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原始数组:\n",
      " [[99  2]\n",
      " [ 3  4]]\n",
      "副本数组:\n",
      " [[ 1  2]\n",
      " [ 3 88]]\n"
     ]
    }
   ],
   "source": [
    "origin = np.array([[1,2],[3,4]])\n",
    "\n",
    "view_arr = origin[0,:]    # 视图（数据共享）\n",
    "copy_arr = origin.copy()  # 深拷贝\n",
    "\n",
    "view_arr[0] = 99          # 修改视图影响原数组\n",
    "copy_arr[1,1] = 88        # 不影响原数据\n",
    "\n",
    "print(\"原始数组:\\n\", origin)  # [[99 2], [3 4]]\n",
    "print(\"副本数组:\\n\", copy_arr)  # [[1 2], [3 88]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 146,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "结构化全1数组: [('1', 1.) ('1', 1.) ('1', 1.)]\n"
     ]
    }
   ],
   "source": [
    "# 复杂数据结构初始化\n",
    "dtype = [('name', 'U10'), ('score', float)]\n",
    "ones_struct = np.ones(3, dtype=dtype)  # 结构化数组\n",
    "print(\"\\n结构化全1数组:\", ones_struct)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 151,
   "metadata": {},
   "outputs": [],
   "source": [
    "ones_struct[0] = ('张三', 59)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 152,
   "metadata": {},
   "outputs": [],
   "source": [
    "ones_struct[1] = ('李四', 58)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 153,
   "metadata": {},
   "outputs": [],
   "source": [
    "ones_struct[2] = ('王五', 57)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 157,
   "metadata": {},
   "outputs": [],
   "source": [
    "ones_struct['score'] = ones_struct['score']**0.5*10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 158,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([('张三', 76.81145748), ('李四', 76.15773106), ('王五', 75.49834435)],\n",
       "      dtype=[('name', '<U10'), ('score', '<f8')])"
      ]
     },
     "execution_count": 158,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ones_struct"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 160,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "标准全1矩阵:\n",
      " [[1. 1. 1.]\n",
      " [1. 1. 1.]\n",
      " [1. 1. 1.]]\n",
      "\n",
      "矩阵运算:\n",
      "[[7. 7. 7.]\n",
      " [7. 7. 7.]\n",
      " [7. 7. 7.]]\n"
     ]
    }
   ],
   "source": [
    "# 单位矩阵替代方案\n",
    "ones_square = np.ones((3,3))  # 3x3全1浮点矩阵\n",
    "print(\"标准全1矩阵:\\n\", ones_square)\n",
    "# 矩阵运算测试\n",
    "print(\"\\n矩阵运算:\")\n",
    "print(ones_square * 5 + 2)  # 广播运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 161,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 网格坐标生成（结合shape）\n",
    "x = np.arange(3)\n",
    "y = np.arange(4)\n",
    "xx, yy = np.meshgrid(x, y)  # 生成网格坐标矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 162,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 2],\n",
       "       [0, 1, 2],\n",
       "       [0, 1, 2],\n",
       "       [0, 1, 2]])"
      ]
     },
     "execution_count": 162,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xx"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 163,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 0, 0],\n",
       "       [1, 1, 1],\n",
       "       [2, 2, 2],\n",
       "       [3, 3, 3]])"
      ]
     },
     "execution_count": 163,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "yy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 164,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.        ,  0.84147098,  0.90929743],\n",
       "       [ 0.        ,  0.45464871,  0.4912955 ],\n",
       "       [-0.        , -0.35017549, -0.37840125],\n",
       "       [-0.        , -0.83304996, -0.90019763]])"
      ]
     },
     "execution_count": 164,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sin(xx) * np.cos(yy)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 165,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 时间序列生成（Pandas扩展更优）\n",
    "time_steps = np.arange('2023-01', '2023-03', dtype='datetime64[D]')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 166,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array(['2023-01-01', '2023-01-02', '2023-01-03', '2023-01-04',\n",
       "       '2023-01-05', '2023-01-06', '2023-01-07', '2023-01-08',\n",
       "       '2023-01-09', '2023-01-10', '2023-01-11', '2023-01-12',\n",
       "       '2023-01-13', '2023-01-14', '2023-01-15', '2023-01-16',\n",
       "       '2023-01-17', '2023-01-18', '2023-01-19', '2023-01-20',\n",
       "       '2023-01-21', '2023-01-22', '2023-01-23', '2023-01-24',\n",
       "       '2023-01-25', '2023-01-26', '2023-01-27', '2023-01-28',\n",
       "       '2023-01-29', '2023-01-30', '2023-01-31', '2023-02-01',\n",
       "       '2023-02-02', '2023-02-03', '2023-02-04', '2023-02-05',\n",
       "       '2023-02-06', '2023-02-07', '2023-02-08', '2023-02-09',\n",
       "       '2023-02-10', '2023-02-11', '2023-02-12', '2023-02-13',\n",
       "       '2023-02-14', '2023-02-15', '2023-02-16', '2023-02-17',\n",
       "       '2023-02-18', '2023-02-19', '2023-02-20', '2023-02-21',\n",
       "       '2023-02-22', '2023-02-23', '2023-02-24', '2023-02-25',\n",
       "       '2023-02-26', '2023-02-27', '2023-02-28'], dtype='datetime64[D]')"
      ]
     },
     "execution_count": 166,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "time_steps"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 167,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "加法:\n",
      " [[11 12 13]\n",
      " [14 15 16]]\n",
      "\n",
      "乘法:\n",
      " [[ 2  4  6]\n",
      " [ 8 10 12]]\n",
      "\n",
      "幂运算:\n",
      " [[ 1  4  9]\n",
      " [16 25 36]]\n",
      "\n",
      "自然对数:\n",
      " [[0.         0.69314718 1.09861229]\n",
      " [1.38629436 1.60943791 1.79175947]]\n",
      "\n",
      "正弦值:\n",
      " [[ 0.84147098  0.90929743  0.14112001]\n",
      " [-0.7568025  -0.95892427 -0.2794155 ]]\n",
      "\n",
      "矩阵转置:\n",
      " [[1 4]\n",
      " [2 5]\n",
      " [3 6]]\n",
      "矩阵乘法:\n",
      " [[14 32]\n",
      " [32 77]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "arr = np.array([[1,2,3], [4,5,6]])\n",
    "\n",
    "# 元素级算术运算\n",
    "print(\"加法:\\n\", arr + 10)       # 所有元素+10\n",
    "print(\"\\n乘法:\\n\", arr * 2)      # 所有元素×2\n",
    "print(\"\\n幂运算:\\n\", arr ** 2)   # 各元素平方\n",
    "\n",
    "# 数学函数应用\n",
    "print(\"\\n自然对数:\\n\", np.log(arr))\n",
    "print(\"\\n正弦值:\\n\", np.sin(arr))\n",
    "\n",
    "# 矩阵运算\n",
    "matrix = arr.reshape(2,3)\n",
    "print(\"\\n矩阵转置:\\n\", matrix.T)       # 3x2转置矩阵\n",
    "print(\"矩阵乘法:\\n\", np.dot(matrix, matrix.T))  # 2x2矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 168,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "矩阵转置:\n",
      " [[1 4]\n",
      " [2 5]\n",
      " [3 6]]\n",
      "矩阵乘法:\n",
      " [[14 32]\n",
      " [32 77]]\n"
     ]
    }
   ],
   "source": [
    "# 矩阵运算\n",
    "matrix = arr.reshape(2,3)\n",
    "print(\"\\n矩阵转置:\\n\", matrix.T)       # 3x2转置矩阵\n",
    "print(\"矩阵乘法:\\n\", np.dot(matrix, matrix.T))  # 2x2矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 171,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[14, 32],\n",
       "       [32, 77]])"
      ]
     },
     "execution_count": 171,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "matrix @ matrix.T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 172,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "广播加法结果:\n",
      " [[ 1  2]\n",
      " [11 12]]\n",
      "\n",
      "三维广播运算:\n",
      " [[[  0 100]]\n",
      "\n",
      " [[ 20 300]]\n",
      "\n",
      " [[ 40 500]]]\n"
     ]
    }
   ],
   "source": [
    "a = np.array([[0,0], [10,10]])  # shape (2,2)\n",
    "b = np.array([1,2])             # shape (2,)\n",
    "\n",
    "# 广播过程：b → [[1,2], [1,2]]\n",
    "result = a + b  \n",
    "print(\"广播加法结果:\\n\", result)\n",
    "\n",
    "# 3维广播示例\n",
    "c = np.arange(6).reshape(3,1,2)  # shape (3,1,2)\n",
    "d = np.array([10,100])          # shape (2,)\n",
    "print(\"\\n三维广播运算:\\n\", c * d)  # 最终形状 (3,1,2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 173,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = np.array([[0,0], [10,10]])  # shape (2,2)\n",
    "b = np.array([1,2])             # shape (2,)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 180,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = np.array([[0,0], [10,10], [20,20]])  # shape (2,2)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 181,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2])"
      ]
     },
     "execution_count": 181,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 186,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  0],\n",
       "       [20, 30],\n",
       "       [40, 60]])"
      ]
     },
     "execution_count": 186,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a * [2,3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 190,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "三维广播运算:\n",
      " [[[ 0  1]\n",
      "  [ 4  6]]\n",
      "\n",
      " [[ 4  5]\n",
      "  [12 14]]\n",
      "\n",
      " [[ 8  9]\n",
      "  [20 22]]\n",
      "\n",
      " [[12 13]\n",
      "  [28 30]]\n",
      "\n",
      " [[16 17]\n",
      "  [36 38]]\n",
      "\n",
      " [[20 21]\n",
      "  [44 46]]]\n"
     ]
    }
   ],
   "source": [
    "c = np.arange(24).reshape(-1,2,2)  # shape (3,1,2)\n",
    "d = np.array([[1,1],[2,2]])          # shape (1,2)\n",
    "print(\"\\n三维广播运算:\\n\", c * d)  # 最终形状 (3,1,2)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('test.txt', 'w+') as f:\n",
    "    f.write('hello')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "f = open('test.txt', 'w+')\n",
    "f.write('hello')\n",
    "f.close()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
